##########################################################################################################
## Payson (2021) When Cities Lobby Replication Code
## Chapter Three Appendix: Tables A3.1 - A3.8, Figure A3.1, On-Line SI
## Created with R version 4.0.3 (2020-10-10) -- "Bunny-Wunnies Freak Out"
##########################################################################################################


## Load packages
library(DataCombine); library(stargazer); library(lfe); 
library(tidyverse); library(devtools); library(fect); 
library(panelView); library(ggpubr)


## Set working directory
setwd()


## Functions for clean tex tables (need to define to compile tables)
pretty <- function(x) {
  paste(prettyNum(x, big.mark = ","))}

num.cities <- function(x) {
  length(which(getfe(x)=="fips"))}

N <- function(x) {
  x$N}

getmean <- function(x) {
  round(mean(x$response), 2)}


##########################################################################################################
## Table A3.1 Summary Statistics
##########################################################################################################

dta <- read.csv("summary_stats_dta.csv")

stargazer(dta, summary.stat = c("n", "mean", "sd", "min", "max"), type = "text", 
          digits = 2, covariate.labels = c("Lobby", "Lobby Expenditures", 
                                           "Population", "Median Income", "Own Source Revenue Per Capita (Log)",
                                           "\\% White", "\\% Unemployed", "Median Home Value",
                                           "Numer Lower House Representatives", "Ideological Distance Between Reps",
                                           "Redistricted", "Delegation Ideology Score",
                                           "City Democratic Vote Share (2008)", "City Ideology Score",
                                           "Democratic City", "Republican City", "Partisan Mismatch"))



##########################################################################################################
## Table A3.2 Correlates of City Lobbying
##########################################################################################################

dta <- read.csv("city_lobby.csv")

m <- felm(lobby ~  lower.num.reps + np.distance + log.pop + pct.white +
            log.income + I(log.income^2) + log.own.source.pp + log.med.house | 
            state + year, data = dta)

m1 <- felm(lobby ~  lower.num.reps + np.distance + log.pop + pct.white +
             log.income + I(log.income^2) + log.own.source.pp + log.med.house | 
             state.by.year, data = dta)

stargazer(m, m1, omit.table.layout = "sn",  type = "text", star.cutoffs = c(0.05), 
          title = c("\\textbf{Correlates of City Lobbying}"),
          dep.var.caption = "Probability of Lobbying", dep.var.labels.include = FALSE,
          covariate.labels = c("\\# House Representatives", "Ideological Distance",
                               "Population (Log)", "\\% White", "Median Income (Log)", 
                               "Median Income Squared (Log)", "Own Source Revenue (Log)", 
                               "Median House Value (Log)"),
          add.lines = list(c("State FEs", "Yes", ""),
                           c("Year FEs", "Yes", ""),
                           c("State-Year FEs", "", "Yes"),
                           c("Observations", pretty(m$N), pretty(m1$N)),
                           c("\\# Cities", pretty(length(unique(dta$fips))), pretty(length(unique(dta$fips)))),
                           c("Mean Lobbying Probability", getmean(m), getmean(m1))))



##########################################################################################################
## Table A3.3 Effect of Gaining Representatives After Redistricting on City Lobbying
##########################################################################################################

dta <- read.csv("city_lobby.csv")

## Create indicator variable that switches to 1 post-2010
dta$post <- ifelse(dta$year > 2010, 1, 0)

## Treatment: redistricted x post
dta$treat1 <- dta$redistricted*dta$post

## Full sample
m <- felm(lobby ~ treat1 | fips + year | 0 | fips, data = dta)

m2 <- felm(lobby ~ treat1 + log.pop + log.income + log.own.source.pp + log.med.house | fips + year | 0 | 
             fips, data = dta)

m3 <- felm(lobby ~ treat1 + log.pop + log.income + log.own.source.pp + log.med.house | fips + state.by.year | 0 | 
             fips, data = dta)

## Large cities only
m4 <- felm(lobby ~ treat1 | fips + year | 0 | fips, data =  subset(dta, pop.cat == "urban"))

m5 <- felm(lobby ~ treat1 + log.pop + log.income + log.own.source.pp + log.med.house | fips + year | 0 | 
             fips, data = 
             subset(dta, pop.cat == "urban"))

m6 <- felm(lobby ~ treat1 + log.pop + log.income + log.own.source.pp + log.med.house | fips + state.by.year | 0 | 
             fips, data = subset(dta, pop.cat == "urban"))


stargazer(m, m2, m3, m4, m5, m6, omit.table.layout = "sn", type = "text",
          star.cutoffs = c(0.05), keep = c(1),
          title = c("\\textbf{Effect of Gaining Representatives After Redistricting on City Lobbying}"),
          dep.var.caption = "Probability of Lobbying", dep.var.labels.include = FALSE,
          column.labels = c("Population > 5,000", "Population > 100,000"), column.separate = c(3, 3),
          covariate.labels = c("Redistricted"),
          add.lines = list(c("City FEs", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes"),
                           c("Year FEs", "Yes", "Yes", "", "Yes", "Yes", ""),
                           c("State-Year FEs", "", "", "Yes", "", "", "Yes"),
                           c("Full Controls", "", "Yes", "Yes", "", "Yes", "Yes"),
                           c("Observations", pretty(m$N), pretty(m2$N), pretty(m3$N),
                             pretty(m4$N), pretty(m5$N), pretty(m6$N)),
                           c("\\# Cities", pretty(num.cities(m)), pretty(num.cities(m2)), 
                             pretty(num.cities(m3)), pretty(num.cities(m4)),
                             pretty(num.cities(m5)), pretty(num.cities(m6))),
                           c("Mean Lobbying Probability", getmean(m), getmean(m2), getmean(m3), 
                             getmean(m4), getmean(m5), getmean(m6))))



###################################################################################################
## On-line SI Table: Leads and Lags on Redistricting. Standard tests show no concerning pretreatment
## trends.
###################################################################################################

dta <- read.csv("city_lobby.csv")

dta$treat <- ifelse(dta$num.reps.lead1 > dta$lower.num.reps & (dta$year == 2011 |
                                                                 dta$year == 2012), 1, 0)

dta <- as.data.frame(dta)

## Create leads and lags of redistricting "treatment"
dta <- slide(dta, Var = "treat", GroupVar = "fips", slideBy = -1, NewVar = "treat.lag1")
dta <- slide(dta, Var = "treat", GroupVar = "fips", slideBy = 1, NewVar = "treat.lead1")
dta <- slide(dta, Var = "treat", GroupVar = "fips", slideBy = 2, NewVar = "treat.lead2")


m <- felm(lobby ~ treat | fips + year | 0 | fips, data = dta)
m1 <- felm(lobby ~ treat.lead1 + treat | fips + year | 0 | fips, data = dta)
m2 <- felm(lobby ~ treat.lead1 + treat + treat.lag1 | fips + year | 0 | fips, data = dta)
m3 <- felm(lobby ~ treat.lead2 + treat.lead1 + treat + treat.lag1 | fips + year | 0 | fips, data = dta)


stargazer(m, m1, m2, m3, omit.table.layout = "s", type = "text", star.cutoffs = c(0.05),
          title = c("\\textbf{Effect of Gaining Representatives After Redistricting: Leads and Lags}"),
          dep.var.caption = "Probability of Lobbying", dep.var.labels.include = FALSE,
          covariate.labels = c("Redistricted, t+2", "Redistricted, t+1", "Redistricted, t", 
                               "Redistricted, t-1"),
          add.lines = list(c("City FEs", "Yes", "Yes", "Yes", "Yes"),
                           c("Year FEs", "Yes", "Yes", "Yes", "Yes"),
                           c("Observations", pretty(m$N), pretty(m1$N), pretty(m2$N), pretty(m3$N)),
                           c("\\# Cities",  pretty(num.cities(m)), pretty(num.cities(m1)), 
                             pretty(num.cities(m2)), pretty(num.cities(m3))),
                           c("Mean Lobbying Probability", getmean(m), getmean(m1), getmean(m2), getmean(m3))),
          notes.append = FALSE, notes.align = "l", notes.label  = "",
          notes = "Robust standard errors clustered by city. $^{*}$p$<$0.05}")



###################################################################################################
## On-line SI Table: Lobbying the Federal Government After Redistricting. Cities are no more likely
## to lobby the federal government after gaining representatives through redistricting.
###################################################################################################

dta <- read.csv("city_lobby.csv")


## Define treatment as gaining representatives in 2011 or 2012 following redistricting
dta$treat <- ifelse(dta$num.reps.lead1 > dta$lower.num.reps & (dta$year == 2011 |
                                                                 dta$year == 2012), 1, 0)

## No effect of redistricting treatment on probability of lobbying federal government
m <- felm(fed.lobby ~ treat + log.pop + log.income + log.own.source.pp + log.med.house | 
            fips + year | 0 | fips, data = dta)
summary(m)

m2 <- felm(fed.lobby ~ treat + log.pop + log.income + log.own.source.pp + log.med.house | 
             fips + state.by.year | 0 | fips, data = dta)
summary(m2)



##########################################################################################################
## Table A3.4 Effect of Delegation Mismatch on City Lobbying
##########################################################################################################

dta <- read.csv("mrp.csv")


m <- felm(lobby ~ mismatch | fips + year | 0 | fips, data = subset(dta, both != "Moderate City"))

m1 <- felm(lobby ~ mismatch + log.pop + log.income + log.own.source.pp + log.med.house |
           fips + year | 0 | fips, data = subset(dta, both != "Moderate City"))

m2 <- felm(lobby ~ mismatch | fips + state.by.year | 0 | fips, data = subset(dta, both != "Moderate City"))

m3 <- felm(lobby ~ mismatch + log.pop + log.income + log.own.source.pp + log.med.house |
           fips + state.by.year | 0 | fips, data = subset(dta, both != "Moderate City"))

stargazer(m, m1, m2, m3, omit.table.layout = "sn", type = "text", star.cutoffs = c(0.05),
          title = c("\\textbf{Effect of Delegation Mismatch on City Lobbying.}"),
          dep.var.caption = "Probability of Lobbying", dep.var.labels.include = FALSE,
          keep = "mismatch", covariate.labels = c("Partisan Mismatch"),
          add.lines = list(c("City FEs", "Yes", "Yes", "Yes", "Yes"),
                           c("Year FEs", "Yes", "Yes", "", ""),
                           c("State-Year FEs", "", "", "Yes", "Yes"),
                           c("Full Controls", "", "Yes", "", "Yes"),
                           c("Observations", pretty(m$N), pretty(m1$N), 
                             pretty(m2$N), pretty(m3$N)),
                           c("\\# Cities",  pretty(num.cities(m)), pretty(num.cities(m1)), 
                             pretty(num.cities(m2)), pretty(num.cities(m3))),
                           c("Mean Lobbying Probability", getmean(m), getmean(m1), 
                             getmean(m2), getmean(m3))))



##########################################################################################################
## On-line SI Table: Leads and Lags on Partisan Mismatch. Standard tests show no concerning pretreatment
## trends.
##########################################################################################################

dta <- read.csv("mrp.csv")

dta <- as.data.frame(dta)
dta <- slide(dta, Var = "mismatch", GroupVar = "fips", slideBy = 1, NewVar = "mismatch.lead1")
dta <- slide(dta, Var = "mismatch", GroupVar = "fips", slideBy = 2, NewVar = "mismatch.lead2")
dta <- slide(dta, Var = "mismatch", GroupVar = "fips", slideBy = -1, NewVar = "mismatch.lag1")
dta <- slide(dta, Var = "mismatch", GroupVar = "fips", slideBy = -2, NewVar = "mismatch.lag2")


m <- felm(lobby ~ mismatch + log.pop + log.income + log.own.source.pp + log.med.house | 
            fips + state.by.year | 0 | fips, 
          data = subset(dta, both != "Moderate"))

m1 <- felm(lobby ~ mismatch.lead1 + mismatch + log.pop + log.income + log.own.source.pp + log.med.house | 
             fips + state.by.year | 0 | fips, 
           data = subset(dta, both != "Moderate"))

m2 <- felm(lobby ~ mismatch.lead1 + mismatch + mismatch.lag1 + log.pop + log.income + log.own.source.pp + log.med.house | 
             fips + state.by.year | 0 | fips, 
           data = subset(dta, both != "Moderate"))

m3 <- felm(lobby ~ mismatch.lead1 + mismatch + mismatch.lag1 + mismatch.lag2 + log.pop + log.income + log.own.source.pp + log.med.house | 
             fips + state.by.year | 0 | fips, 
           data = subset(dta, both != "Moderate"))

stargazer(m, m1, m2, m3, omit.table.layout = "s", type = "text", 
          title = c("\\textbf{Effect of Partisan Mismatch on City Lobbying: Leads and Lags}"),
          dep.var.caption = "Probability of Lobbying", dep.var.labels.include = FALSE,
          keep = c("mismatch.lead1", "mismatch", "mismatch.lag1", "mismatch.lag2"),
          covariate.labels = c("Partisan Mismatch, t+1", "Partisan Mismatch, t", "Partisan Mismatch, t-1", 
                               "Partisan Mismatch, t-2"),
          add.lines = list(c("City FEs", "Yes", "Yes", "Yes", "Yes"),
                           c("State-Year FEs", "Yes", "Yes", "Yes", "Yes"),
                           c("Full Controls", "Yes", "Yes", "Yes", "Yes"),
                           c("Observations", pretty(m$N), pretty(m1$N), pretty(m2$N), pretty(m3$N)),
                           c("\\# Cities", pretty(num.cities(m)), pretty(num.cities(m1)), pretty(num.cities(m2)), 
                             pretty(num.cities(m3))),
                           c("Mean Lobbying Probability", getmean(m), getmean(m1), getmean(m2), getmean(m3))))



##########################################################################################################
## Table A3.5 Effect of Delegation Mismatch by City Size
##########################################################################################################

dta <- read.csv("mrp.csv")

m <- felm(lobby ~ mismatch + log.pop + log.income + log.own.source.pp + log.med.house
          | fips + year | 0 | fips, data = subset(dta, both != "Moderate City"))

m1 <- felm(lobby ~ mismatch + log.pop + log.income + log.own.source.pp + log.med.house
           | fips + year | 0 | fips, data = subset(dta, both != "Moderate City" & pop.cat == "medium"))


m2 <- felm(lobby ~ mismatch + log.pop + log.income + log.own.source.pp + log.med.house
           | fips + year | 0 | fips, data = subset(dta, both != "Moderate City" & pop.cat == "big"))


m3 <- felm(lobby ~ mismatch + log.pop + log.income + log.own.source.pp + log.med.house
           | fips + year | 0 | fips, data = subset(dta, both != "Moderate City" & pop.cat == "urban"))


stargazer(m, m1, m2, m3, omit.table.layout = "sn", type = "text", star.cutoffs = c(0.05),
          title = c("\\textbf{Effect of Partisan Mismatch on Lobbying By City Size.}"),
          dep.var.caption = "Probability of Lobbying", dep.var.labels.include = FALSE,
          column.labels = c("Full Sample", "20,000 - 50,000", "50,000 - 100,000", "Over 100,000"), 
          keep = "mismatch", covariate.labels = c("Partisan Mismatch"),
          add.lines = list(c("City FEs", "Yes", "Yes", "Yes", "Yes"),
                           c("Year FEs", "Yes", "Yes", "Yes", "Yes"),
                           c("Full Controls", "Yes", "Yes", "Yes", "Yes"),
                           c("Observations", pretty(m$N), pretty(m1$N), 
                             pretty(m2$N), pretty(m3$N)),
                           c("\\# Cities",  pretty(num.cities(m)), pretty(num.cities(m1)), 
                             pretty(num.cities(m2)), pretty(num.cities(m3))),
                           c("Mean Lobbying Probability", getmean(m), getmean(m1), 
                             getmean(m2), getmean(m3))))



##########################################################################################################
## Table A3.6 Effect of Delegation Mismatch on Lobbying Expenditures
##########################################################################################################

dta <- read.csv("mrp.csv")

exp.states <- dta[dta$exp.dta==1,]

## Drop extremeley low expenditure reports and log
exp.states$amount <- ifelse(exp.states$amount < 1000, 0, exp.states$amount)
exp.states$amount.log <- log(exp.states$amount + 1)


m <- felm(amount.log ~ mismatch + log.pop + log.income + log.own.source.pp + log.med.house | 
            fips + state.by.year | 0 | fips, data = subset(exp.states, amount.log < 12))

m1 <- felm(amount.log ~ mismatch +log.pop + log.income + log.own.source.pp + log.med.house | 
             fips + state.by.year | 0 | fips, data = subset(exp.states, amount.log < 12 &
                                                              pop.cat=="medium"))

m2 <- felm(amount.log ~ mismatch + log.pop + log.income + log.own.source.pp + log.med.house | 
             fips + state.by.year | 0 | fips, data = subset(exp.states,amount.log < 12 &
                                                              pop.cat=="big"))

m3 <- felm(amount.log ~ mismatch + log.pop + log.income + log.own.source.pp + log.med.house | 
             fips + state.by.year | 0 | fips, data = subset(exp.states, amount.log < 12 &
                                                              pop.cat=="urban"))


stargazer(m, m1, m2, m3, omit.table.layout = "sn", type = "text", star.cutoffs = c(0.05), 
          title = c("\\textbf{Effect of Partisanship Mismatch on Lobbying Expenditures}"),
          dep.var.caption = "Expenditures on Lobbying (Log)", dep.var.labels.include = FALSE,
          column.labels = c("Full Sample", "20,000 - 50,000", "50,000 - 100,000", "Over 100,000"), 
          keep = "mismatch", covariate.labels = c("Partisanship Mismatch"),
          add.lines = list(c("City FEs", "Yes", "Yes", "Yes", "Yes"),
                           c("State-Year FEs", "Yes", "Yes", "Yes", "Yes"),
                           c("Observations", pretty(m$N), pretty(m1$N), pretty(m2$N), pretty(m3$N)),
                           c("\\# Cities",  pretty(num.cities(m)), pretty(num.cities(m1)), 
                             pretty(num.cities(m2)), pretty(num.cities(m3))),
                           c("Mean Log Exp.", getmean(m), getmean(m1), getmean(m2), getmean(m3))))



##########################################################################################################
## Table A3.7 Effect of Delegation Partisanship vs. Ideology on City Lobbying
##########################################################################################################

dta <- read.csv("mrp.csv")

m <- felm(lobby ~ mismatch + log.pop + log.income + log.own.source.pp + log.med.house
          | fips + year | 0 | fips, data = subset(dta, both == "Democratic City"))

m1 <- felm(lobby ~ lower.mean.np + log.pop + log.income + log.own.source.pp + log.med.house | fips + year, 
           data = subset(dta, both == "Democratic City"))

m2 <- felm(lobby ~ mismatch + lower.mean.np + log.pop + log.income + log.own.source.pp + log.med.house
           | fips + year, data = subset(dta, both == "Democratic City"))


m3 <- felm(lobby ~ mismatch + log.pop + log.income + log.own.source.pp + log.med.house
           | fips + year | 0 | fips, data = subset(dta, both == "Republican City"))

m4 <- felm(lobby ~ lower.mean.np + log.pop + log.income + log.own.source.pp + log.med.house
           | fips + year | 0 | fips, data = subset(dta, both == "Republican City"))

m5 <- felm(lobby ~ mismatch + lower.mean.np + log.pop + log.income + log.own.source.pp + log.med.house
           | fips + year | 0 | fips, data = subset(dta, both == "Republican City"))



stargazer(m, m1, m2, m3, m4, m5, omit.table.layout = "sn", type = "text",star.cutoffs = c(0.05),
          column.labels = c("Democratic City", "Republican City"), column.separate = c(3, 3),
          title = c("\\textbf{Effect of Delegation Partisanship vs. Ideology on City Lobbying}"),
          dep.var.caption = "Probability of Lobbying", dep.var.labels.include = FALSE,
          keep = c("lower.mean.np", "mismatch"),
          covariate.labels = c("Partisan Mismatch", "Conservative Ideology"),
          add.lines = list(c("City FEs", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes"),
                           c("Year FEs", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes"),
                           c("Full Controls", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes"),
                           c("Observations", pretty(m$N), pretty(m1$N), pretty(m2$N), pretty(m3$N),
                             pretty(m4$N), pretty(m5$N)),
                           c("\\# Cities", pretty(num.cities(m)), pretty(num.cities(m1)), pretty(num.cities(m2)), 
                             pretty(num.cities(m3)), pretty(num.cities(m4)), pretty(num.cities(m5))),
                           c("Mean Lobbying Probability", getmean(m), getmean(m1), getmean(m2), getmean(m3),
                             getmean(m4), getmean(m5))))



##########################################################################################################
## Table A3.8 Effect of Delegation vs. Chamber Mismatch on City Lobbying
##########################################################################################################

dta <- read.csv("mrp.csv")

dta$chamber.mismatch <- ifelse((dta$both == "Democratic City" & dta$house.rep.prop > .5) |
                                 (dta$both == "Republican City" & dta$house.dem.prop > .5), 1, 0)


m <- felm(lobby ~ chamber.mismatch + log.pop + log.own.source + log.income +
            log.med.house | fips + state.by.year | 0 | fips, 
          data = subset(dta, dta$both != "Moderate City"))

m1 <- felm(lobby ~ chamber.mismatch + mismatch + log.pop + log.own.source + log.income +
             log.med.house | fips + state.by.year | 0 | fips, 
           data = subset(dta, dta$both != "Moderate City"))

stargazer(m, m1, omit.table.layout = "sn", type = "text",
          star.cutoffs = c(0.05), font.size = "small",
          table.placement = "t", label = "table:chamber",
          title = c("\\textbf{Effect of Delegation vs. Chamber Mismatch on City Lobbying.}"),
          dep.var.caption = "Probability of Lobbying", dep.var.labels.include = FALSE,
          keep = c("chamber.mismatch", "mismatch"),
          covariate.labels = c("Chamber Mismatch", "Delegation Mismatch"),
          add.lines = list(c("City FEs", "Yes", "Yes"),
                           c("State-Year FEs", "Yes", "Yes"),
                           c("Full Controls", "Yes", "Yes"),
                           c("Observations", pretty(m$N), pretty(m1$N)),
                           c("\\# Cities", pretty(num.cities(m)), pretty(num.cities(m1))),
                           c("Mean Lobbying Probability", getmean(m), getmean(m1))))



##########################################################################################################
## Figure A3.1 Effect of Becoming Mismatched with State Delegation: Leads and Lags. 
##########################################################################################################

dta <- read.csv("mrp.csv")

tmp <- subset(dta, both!="Moderate City" & !is.na(mismatch))
tmp$mismatch <- as.numeric(as.character(tmp$mismatch))

out.fect <- fect(lobby ~ mismatch + log.pop, data = tmp, index = c("fips","year"), 
                 force = "two-way", se = TRUE)

plot(out.fect)

plot.dta <- plot(out.fect, xlim=c(-2, 2))$data

lower <- plot.dta$ATT - (plot.dta$S.E.*1.8)
upper <- plot.dta$ATT + (plot.dta$S.E.*1.8)

limits <- aes(ymin = lower , ymax = upper)

(p <- ggplot(plot.dta, aes(time, ATT)) +
    geom_errorbar(limits, width = 0.1) +
    geom_line() +
    geom_hline(yintercept = 0) +
    geom_vline(xintercept = 0, linetype = 2) +
    theme_bw(base_family = "serif", base_size = 12) +
    theme(panel.spacing = unit(2, "lines"),
          plot.margin = unit(c(1,2,1,1), "cm"),
          plot.background = element_blank(),
          panel.grid.major = element_blank(),
          panel.grid.minor = element_blank(),
          strip.background = element_rect(fill = "white")) +
    labs(y = "Effect on Probability of Lobbying", x = "Years Since Mismatch"))
